package overall.manager
{
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	
	import overall.event.ServerDataEvent;
	import overall.message.DemoMsg;
	import overall.message.PingMsg;
	import overall.message.game.GameOperateNotifyMsg;
	import overall.message.game.GameOperateResultMsg;
	import overall.message.game.GameOutCardMsg;
	import overall.message.game.GameSendCardMsg;
	import overall.message.game.GameStartMsg;
	import overall.message.login.LoginResult;
	import overall.message.room.GetUserListMsg;
	import overall.message.room.RoomBroadcastMsg;
	import overall.message.room.RoomChangeDeskMsg;
	import overall.message.room.RoomConfigMsg;
	import overall.message.room.RoomJoinDeskMsg;
	import overall.message.room.RoomReadyMsg;
	import overall.webPackage.WebDataManager;
	import overall.webPackage.WebURLLoader;
	
	import xnet.Message.MessageBase;
	import xnet.Message.MessageFactory;
	import xnet.SocketManager;
	import xnet.core.NetService;
	import xnet.events.SocketEvent;


	public class ServerDataManager extends EventDispatcher
	{
		public function ServerDataManager()
		{
		}
		static private var _instance:ServerDataManager;
		static public function get instance():ServerDataManager{
			_instance=_instance||new ServerDataManager;
			return _instance;
		}
		
		private var _socket:NetService;

		private var _webLoad:WebURLLoader;
		public function start():void
		{
			MessageFactory.handler=ecode;
			WebDataManager.instance.handler=ecode;
			_addEvent();
			_webLoad=new WebURLLoader(Config.WEBURL);
			_socket=new NetService(Config.IP,Config.POST);
			_socket.connect();
		}
		private function _addEvent():void
		{
			SocketManager.instance.addEventListener(SocketEvent.CONNECT,_socketConnect)
			WebDataManager.instance.addEventListener(ServerDataEvent.ServerDataCom,_webDataCom)
		}
		private function _socketConnect(e:SocketEvent):void
		{
            this.dispatchEvent(new SocketEvent(SocketEvent.CONNECT));
		}
		public var token:String
		public function pingStart(str:String):void{
			token=str;
			var time:Timer=new Timer(10000);
			time.addEventListener(TimerEvent.TIMER,_timerEvt);
			time.start();
		}
		private function _timerEvt(e:TimerEvent):void
		{
			var pkg:ByteArray=new ByteArray;
			pkg.writeByte(AgreementCmd.SEND_PingMsg);
			pkg.writeUTF(token);
			sendData(pkg);
		}
		/**
		 *注入消息工厂函数 
		 * @param cmd
		 * @param msg
		 * 
		 */		
		static public  function ecode(cmd:int,msg:MessageBase):MessageBase
		{
			msg=new MessageBase;
			switch(cmd)
			{
				case AgreementCmd.GET_PingMsg:
					msg = new PingMsg();
					break;
				case AgreementCmd.L_GET_LoginToServer:
					msg = new LoginResult();
					break;
				case AgreementCmd.R_GET_IntoRoom:
					msg = new RoomConfigMsg();
					break;
				case AgreementCmd.R_GET_GetUserList:
					msg = new GetUserListMsg();
					break;
				case AgreementCmd.R_GET_Broadcast:
					msg = new RoomBroadcastMsg();
					break;
				case AgreementCmd.R_GET_JoinDesk:
					msg=new RoomJoinDeskMsg;
					break;
				case AgreementCmd.R_GET_ExitDesk:
					msg=new RoomChangeDeskMsg;
					break;
				case AgreementCmd.R_GET_Ready:
					msg=new RoomReadyMsg();
					break;
				case AgreementCmd.G_GET_GameStart:
					msg=new GameStartMsg();
					break;
				case AgreementCmd.G_GET_OutCard:
					msg=new GameOutCardMsg();
					break;
				case AgreementCmd.G_GET_SendCard:
					msg=new GameSendCardMsg();
					break;
				case AgreementCmd.G_OperateNotify:
					msg=new GameOperateNotifyMsg();
					break;
				case AgreementCmd.G_OperateResulf:
					msg=new GameOperateResultMsg();
					break;
				case AgreementCmd.Web_Demo:
					msg=new DemoMsg();
					break;
			}
			return msg
		}
		
		
		
		
		//---------------------------------------消息发送及回调函数----------------------------
		
		public function sendData(data:ByteArray):void
		{
			_socket.send(data,dataCom);
		}
		
		private function dataCom(data:MessageBase):void
		{
			dispatchEvent(new ServerDataEvent(ServerDataEvent.ServerDataCom,data));

		}
		
		
		public function sendWebData(data:ByteArray):void
		{
			_webLoad.send(data);
		}
		private function _webDataCom(e:ServerDataEvent):void
		{
			var msg:MessageBase=MessageBase(e.info);
			dataCom(msg);
		}
		
		
		
		
	}
}